package org.spiderflow.core.utils;

import org.spiderflow.common.executor.ShapeExecutor;
import org.spiderflow.common.model.Shape;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created on 2020-03-11
 *
 * @author Octopus
 */
public class ExecutorsUtils {

    /**
     * 节点执行器列表 当前爬虫的全部流程
     */
    private static final Collection<ShapeExecutor> EXECUTORS = SpringUtils.getBeansCollectionOfType(ShapeExecutor.class);

    private ExecutorsUtils() {
    }

    public static List<Shape> shapes() {
        return EXECUTORS.stream().map(ShapeExecutor::shape)
                        .filter(Objects::nonNull).collect(Collectors.toList());
    }

    public static Optional<ShapeExecutor> get(String shape) {
        return EXECUTORS.stream()
                        .filter(executor -> executor.isMatch(shape))
                        .findAny();
    }
}
